<?php
error_reporting(E_ALL);
session_start();
include_once("../inc/config.inc.php");
include_once("../inc/admin.functions.php");
include_once("../inc/difference.function.php");

if(($_SERVER['PHP_SELF'] != '/client/index.php') && isset($_SESSION['loggedin']) && $_SESSION['loggedin'] != '1234'){
  header("Location: ".$domain."index.php?l=no");
  exit();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Welcome, <?=  disp_full_name_admin(); ?> - Client Intranet</title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-2" />
<link rel="stylesheet" href="../css/screen.css" type="text/css" media="screen, projection"/>
<!--[if IE]><link rel="stylesheet" href="../css/ie.css" type="text/css" media="screen, projection"><![endif]-->
<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../js/jquery.metadata.js" type="text/javascript"></script>
<script src="../js/jquery.validate.pack.js" type="text/javascript"></script>
<script src="../js/jquery.form.js" type="text/javascript"></script>
<script src="../js/jquery.edit-inplace.js" type="text/javascript"></script>
<script src="../js/jquery.upload.js" type="text/javascript"></script>
<script src="../js/jquery.truncate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){

	// used for validation - rules are set in validate="XX"
	$.metadata.setType("attr", "validate");
	
	// validate add project form
	$("form#add-project").validate({
		rules: {
			c_id: "required",
			cat_id: "required",
			p_title: "required",
			p_url:  {required:true,url:true},
			username: "required",			
			password: "required",
			ftp_address: "required",
			ftp_username: "required",
			ftp_password: "required",
			host_name: "required",
			host_address: {required:true,url:true},
			host_username: "required",
			host_password: "required",
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				success: function(){
					$('form#add-project').hide();
                    $('div.success').fadeIn();
				}
			});
		}
	});
	
	// validate add client form
	$("form#add-client").validate({
		rules: {
			fname: "required",
			lname: "required",
			email: {required:true,email:true},
			lname: "required",	
			username: "required",			
			password: "required",
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				success: function(){
					$('form#add-client').hide();
                    $('div.success').fadeIn();
				}
			});
		}
	});
	// upload invoice validation & ajax
	$("form#uploadinvoice").validate({
		rules: {
			title: "required",
			c_id: "required",
			invoices: "required",
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit({
 				success: function(data){
					$('form#uploadinvoice').hide("fast",
						function showSuccess(){
							$('div.success').fadeIn();
						});
				}
			});
		}
	});

	$('.project').hide();
	$('#c_id').change(function() {
        var client_id = $(this).val();
        $('.project').show();
        $.getJSON("../inc/get-projects.php", {id: client_id}, function(data){
                        projects = $('#p_id');
                        projects.empty();
                        $.each(data, function() {
                        var option = $('<option/>').attr('value', this.id).text(this.name);
                        projects.append(option);
                        });
                });
    })
	
	$(".fname,.lname,.sname,.surl,.email,.username,.password,.comments").editInPlace({
		url: "inc/update.php"
	});
	$(".status").editInPlace({
		url: "inc/updateStatus.php",
		default_text:  "(Add project status here)"
	});
	$('input#invoices').MultiFile({
		accept:'jpg|pdf|doc', max:1, STRING:{
			remove:'Remove file',
			selected:'Selected: $file',
			denied:'Invalid file type $ext!',
			duplicate:'File already exists:\n$file!'
			}
	});
	$('#n').jTruncate({length:50});	
	
	
	$('form#updateclient select').change(updateClient);
	$('form#updateclient input').blur(updateClient);
	$("form#updateclient").validate({
		rules: {
			minutes: {digits:true}
		}
	});
	
	function updateClient(){
	  var data ={
	    "completed" : $('#completed').is(':checked') ? '1' : '0',
	    "minutes" : $('#minutes').val(),
	    "u_id" : $("#u_id").val(),
	    "p_id" : $("#p_id").val(),
	    "who" : $("select#who").val()
	  };
	  
		if(data.who != 0){
			$.ajax({
				url: '../inc/process-update.php',
				type: 'post',
				dataType: 'json',
				data: data,
				success: function(data){
					$('#minutes').val(data.minutes);
					$('#hours').val(data.hours);
					$('#completed').attr('checked', data.completed == '1' ? 'checked' : '');
				}
			});
		}
		
	};
	$.getJSON('../inc/get-update-data.php',function(data) {
		$('#minutes').val(data.minutes);
		$('#hours').val(data.hours);
		$('#completed').attr('checked', data.completed == '1' ? 'checked' : '');
	});

/*

OLD CODE!!!

	function updateClient(){
		var data = {
			"completed" : $('#completed').is(':checked') ? '1' : '0',
			"minutes" : $('#minutes').val(),
			"u_id" : $("#u_id").val(),
			"p_id" : $("#p_id").val(),
			"who" : $("select#who").val()
		};
		
		$.ajax({
			url: '../inc/process-update.php',
			type: 'post',
			dataType: 'json',
			data: data,
			success: function (data) {
				$('#minutes').val(data.minutes);
				$('#hours').val(data.hours);
				$('#completed').attr('checked', data.completed == '1' ? 'checked' : '');

				$('.success').fadeIn();alert

			}
		});
	};	
	
	*/
});
</script>
</head>
<body>
<div class="container">
<div class="span-24">
		<h1>Welcome <?= disp_full_name_admin(); ?></h1>
		<h2 class="alt">Keep up to date with your clients by mananging them with the links below.</h2>
		<hr />

	</div>
	<div class="span-4 colborder">
		<ul id="sort-menu">
			<li> <a href="index.php" title="Go home">Home</a> </li>
			<li> <a href="?c=addclient" title="Add Client">Add Client</a> </li>
			<li> <a href="?c=addproject" title="Add Project">Add Project</a> </li>
			<li> <a href="?c=message&m=yes" title="Send Message">Messages</a> </li>
				<?php if(isset($_GET["m"]) && $_GET["m"] == 'yes'){
					echo "
				<ul style='margin-bottom:0px'>
					<li><a href='?c=message&p=send&m=yes'>Send</a></li>
					<li><a href='?c=message&p=new&m=yes'>New</a></li>
					<li><a href='?c=message&p=sent&m=yes'>Sent</a></li>
					<li><a href='?c=message&p=read&m=yes'>Read</a></li>
					<li><a href='?c=message&p=deleted&m=yes'>Deleted</a></li>
				</ul>
				";}?>
			<li> <a href="?c=uploadinvoice" title="Upload Invoice">Upload Invoice</a> </li>
			<li> <a href="?c=viewclients" title="View Clients">View Clients (<?= client_count(); ?>)</a> </li>
			<li> <a href="?c=viewprojects" title="View Projects">View Projects (<?= project_count(); ?>)</a> </li>
			<li> <a href="?c=viewupdates" title="View Updates">View Updates (<?= client_update_count(); ?>)</a> </li>
            <li> <a href="?c=viewtasks" title="Set Client Status">Tasks (0)</a> </li>
			<li> <a href="<?= $domain ?>/?c=logout" title="Logout">Logout</a> </li>
		</ul>
	</div>
		<div class="span-16">